package fr.neyb.bernouilli.common.tools;

public class BinaryTools {
	private BinaryTools() {
	}

	public static int binaryDigitsNum(long l) {
		if (l == 0)
			return -1;

		final int maxLength = 64;
		int min = 0, max = maxLength, mid = max / 2;

		for (; max - 1 > min; mid = (max + min) / 2) {
			if ((binaryFill(mid, max - 1) & l) != 0) {
				min = mid;
			} else {
				max = mid;
			}
		}

		return min;

	}

	public static long binaryFill(int from, int to) {
		long result = 0;

		for (int i = from; i <= to; i++) {
			result |= 1l << i;
		}

		return result;
	}

}
